package utils;

import java.security.NoSuchAlgorithmException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;

import models.Tweet;

public class DAOTweet extends DAO {

	
	public DAOTweet() throws Exception{
		super();
	}
	
	public void addNewTweet(String user, String tweet) throws SQLException, NoSuchAlgorithmException{
		Statement sentencia = connexio.createStatement();
		String query = "INSERT INTO `ts1`.`tweet` (`id_user`, `tweet`, datetime) VALUES ('"+user+ "', '"+ tweet+"', NOW())";
		sentencia.execute(query);
	}
	
	public ArrayList<Tweet> getUserTweets(String user, int numTweets) throws SQLException, NoSuchAlgorithmException{
		ArrayList<Tweet> tweets = new ArrayList<Tweet>();
		Statement sentencia = connexio.createStatement();
		try{
			String query = "SELECT * FROM `ts1`.`tweet` WHERE id_user ='" + user + "' ORDER BY datetime DESC limit " + numTweets + ";";
			ResultSet result = sentencia.executeQuery(query);
			while(result.next()) {
				int idTweet = Integer.parseInt(result.getString("idtweet"));
				String iduser = result.getString("id_user");
				String tweetContent = result.getString("tweet");
				Timestamp data = result.getTimestamp("datetime");
				boolean allowComments = result.getBoolean("allowComments");

				Tweet tweet = new Tweet(idTweet,iduser, tweetContent ,data ,allowComments);
				tweets.add(tweet);
			}
		}		
		 catch (Exception ex){
			ex.printStackTrace();
		}
		
		return tweets;
	}
	
	public ArrayList<Tweet> getAllUserTweets(String user, int numTweets) throws SQLException, NoSuchAlgorithmException{
		ArrayList<Tweet> tweets = new ArrayList<Tweet>();
		Statement sentencia = connexio.createStatement();
		try{
			String query = "select * from ts1.tweet t where t.id_user = '" + user + "' OR t.id_user IN (select following from ts1.follow f where f.follower = '" + user + "') ORDER BY t.datetime DESC limit " + numTweets + ";";
			ResultSet result = sentencia.executeQuery(query);
			while(result.next()) {
				int idTweet = Integer.parseInt(result.getString("idtweet"));
				String iduser = result.getString("id_user");
				String tweetContent = result.getString("tweet");
				Timestamp data = result.getTimestamp("datetime");
				boolean allowComments = result.getBoolean("allowComments");

				Tweet tweet = new Tweet(idTweet,iduser, tweetContent ,data ,allowComments);
				tweets.add(tweet);
			}
		}		
		 catch (Exception ex){
			ex.printStackTrace();
		}
		 
		return tweets;
	}
	

	public void deleteTweet(int tweetID) throws SQLException, NoSuchAlgorithmException {
		Statement sentencia = connexio.createStatement();
		String query = "DELETE FROM tweet WHERE idtweet = '"+ tweetID+"'";
		sentencia.execute(query);
	}
	
	public void setAllowComments(int tweetID) throws SQLException, NoSuchAlgorithmException {
		Statement sentencia = connexio.createStatement();
		String query = "UPDATE tweet set allowComments = 1 WHERE idtweet = '"+ tweetID+"'";
		sentencia.execute(query);
	}
	
	public void setDisallowComments(int tweetID) throws SQLException, NoSuchAlgorithmException {
		Statement sentencia = connexio.createStatement();
		String query = "UPDATE tweet set allowComments = 0 WHERE idtweet = '"+ tweetID+"'";
		sentencia.execute(query);
	}

	public Tweet getTweet(int tweetId) throws SQLException {
		Statement sentencia = connexio.createStatement();
		Tweet tweet = null;
		String query = "select * from ts1.tweet t where t.idtweet =" + tweetId + ";";
		ResultSet result = sentencia.executeQuery(query);
		if(result.next()) {
			int idTweet = Integer.parseInt(result.getString("idtweet"));
			String iduser = result.getString("id_user");
			String tweetContent = result.getString("tweet");
			Timestamp data = result.getTimestamp("datetime");
			boolean allowComments = result.getBoolean("allowComments");

			tweet = new Tweet(idTweet,iduser, tweetContent ,data ,allowComments);

		}
		return tweet;
	}
}
